﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
namespace NotificationService.DAL
{
    class DataAccessHelper
    {
        #region "Variables"
        private SqlConnection conn;
        private SqlCommand cmd;
        private SqlDataAdapter da;
        private string connectionString;
        #endregion

        #region "Constructors"
        public DataAccessHelper()
        {
            connectionString = @"Data Source=MAIL-ZPNY5SMQBU;Initial Catalog=Ringtones;uid=sa;pwd=Mimasdev%";
            connectionString = @"Data Source=localhost;Initial Catalog=Ringtones;Integrated Security=true";
            conn = new SqlConnection(connectionString);
            cmd = conn.CreateCommand();
            da = new SqlDataAdapter();
        }
        public DataAccessHelper(string strConn)
        {
            conn = new SqlConnection(strConn);
            cmd = conn.CreateCommand();
            da = new SqlDataAdapter();
        } 
        #endregion

        #region "Methods"
        public void Open()
        {
            if (conn.State == ConnectionState.Closed)
            {
                conn.Open();
            }
        }

        public void Close()
        {
            if (conn.State == ConnectionState.Open)
            {
                conn.Close();
                conn.Dispose();
                conn = null;
            }
        }

        public object ExecuteScalar(string query)
        {
            Open();
            cmd.CommandText = query;
            return cmd.ExecuteScalar();
        }

        public DataTable GetDataTable(string select)
        {
            cmd.CommandText = select;
            da.SelectCommand = cmd;
            DataTable dt = new DataTable();
            da.Fill(dt);
            return dt;
        }

        public int ExecuteNonQuery(string query)
        {
            Open();
            cmd.CommandText = query;
            return cmd.ExecuteNonQuery();
        }
        public SqlDataReader ExecuteReader(string query)
        {
            Open();
            cmd.CommandText = query;
            return cmd.ExecuteReader();
        }
        #endregion
    }
}
